package com.boatAnchorAssistant.service.listener;

import com.boatAnchorAssistant.pojo.dto.user.CreateAdminUserDto;
import com.boatAnchorAssistant.pojo.dto.user.MoveUserDto;
import com.boatAnchorAssistant.pojo.event.department.DeleteDepartmentEvent;
import com.boatAnchorAssistant.pojo.event.tenant.CreateTenantEvent;
import com.boatAnchorAssistant.pojo.event.tenant.DeleteTenantEvent;
import com.boatAnchorAssistant.service.service.in.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Joseph.Li
 * @author joseph.li@king-v.com
 * Created in 2022-06-26 16:43
 */
@Service
public class UserListener {


    private final UserService userService;


    public UserListener(UserService userService) {
        this.userService = userService;
    }

    /**
     *
     * 在创建企业后，需要创建系统管理员用户
     *
     * @param event
     */
    @EventListener(CreateTenantEvent.class)
    public void  userListenerCreateTenantEvent(CreateTenantEvent event){
        CreateAdminUserDto dto = CreateAdminUserDto.builder()
                .tenantId(event.getTenantId())
                .departmentId(event.getDepartmentId())
                .account(event.getAdminAccount())
                .email(event.getEmail())
                .password(event.getPassword())
                .adminUserId(event.getAdminUserId())
                .build();

//        userService.createAdminUser(dto);
    }


    /**
     * 监听删除企业事件
     *
     * 删除企业下的全部用户
     * @param event
     */
    @EventListener(DeleteTenantEvent.class)
    public void userListener(DeleteTenantEvent event){
        List<Long> idList = event.getUserIdList();
        userService.bathRemoveUser(idList);
    }


    /**
     * 监听删除部门事件
     *
     * 将所有的下级成员全部迁移至顶级部门之下
     * @param event
     */
    @EventListener(DeleteDepartmentEvent.class)
    public void userListener(DeleteDepartmentEvent event){

        MoveUserDto dto=new MoveUserDto();
        BeanUtils.copyProperties(event,dto);

        userService.bathMoveUser(dto);
    }

}
